package tr.edu.metu.is.mega.client;

import tr.edu.metu.is.mega.client.core.MainWindow;
import tr.edu.metu.is.mega.client.core.ScreenMediator;
import tr.edu.metu.is.mega.client.menu.MenuWindowFactory;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.History;

/**
 * MEGA applications' main frame.
 */
public class MEGAMainFrame extends ContentPanel implements ValueChangeHandler<String> {

    private ContentPanel windowContainer;

    /**
     * Default constructor.
     */
    public MEGAMainFrame() {
        setHeaderVisible(false);
        setBorders(false);
        setBodyBorder(false);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onRender(Element parent, int pos) {
        super.onRender(parent, pos);
        this.setLayout(new BorderLayout());

        windowContainer = new ContentPanel();
        windowContainer.setHeaderVisible(false);
        windowContainer.setBorders(false);
        windowContainer.setBodyBorder(false);
        windowContainer.setLayout(new FitLayout());
        // set window container instance to screen mediator
        ScreenMediator.getInstance().setContainer(windowContainer);

        HeaderPanel panelHeader = new HeaderPanel();

        BorderLayoutData layoutDataTop = new BorderLayoutData(LayoutRegion.NORTH, 30);
        layoutDataTop.setSplit(true);
        layoutDataTop.setCollapsible(true);
        layoutDataTop.setMargins(new Margins(0, 0, 0, 0));
        this.add(panelHeader, layoutDataTop);

        BorderLayoutData layoutDataCenter = new BorderLayoutData(LayoutRegion.CENTER);
        layoutDataCenter.setSplit(true);
        layoutDataCenter.setCollapsible(false);
        layoutDataCenter.setMargins(new Margins(0, 0, 0, 0));
        this.add(windowContainer, layoutDataCenter);
        // show default screen (dashboard)

        History.addValueChangeHandler(this);
        History.fireCurrentHistoryState();
        UserDashboardWindow window = new UserDashboardWindow();
        windowContainer.add(window);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onValueChange(ValueChangeEvent<String> event) {
        MainWindow window = MenuWindowFactory.getInstance(History.getToken());
        ScreenMediator.getInstance().display(window);
    }

}
